package cn.codesea.housework_user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.EvaListRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickOrderListener;
import cn.codesea.housework_user.entity.Order;
import cn.codesea.housework_user.entity.Worker;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.WorkerService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 评价列表
 */
public class EvalutionListActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    EvaListRcvAdapter adapter;
    List<Order> orders = new ArrayList<>();
    private LinearLayoutManager mLinearLayoutManager;

    Worker worker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evalutionlist);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        initData();
        initView();
    }

    private void initData() {
        Intent data = getIntent();
        worker = (Worker) data.getSerializableExtra("worker");

        WorkerService service = ServiceGenerator.createService(WorkerService.class);
        Call<Result> call = service.getEvaluation(worker.getWorkerid());
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    private void initView(){

        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        adapter = new EvaListRcvAdapter(this,orders);
        adapter.setRecyclerItemClickListener(new OnRecyclerItemClickOrderListener() {
            @Override
            public void onItemClick(int Position, List<Order> list) {

            }
        });
        mLinearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result){
        Type type = new TypeToken<List<Order>>(){}.getType();
        orders.clear();
        orders.addAll((List<Order>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        adapter.notifyDataSetChanged();
    }
}
